package com.encyclopedia.controller;

import com.encyclopedia.domain.vo.PlanVo;
import com.encyclopedia.domain.vo.PlantInfoVo;
import com.encyclopedia.result.JsonRequest;
import com.encyclopedia.service.AdminPlantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author MaLiang
 * @version 1.0
 * @date 2024-04-28 19:39
 */
@RestController
@RequestMapping("/admin/plant")
@Slf4j
public class AdminPlantController {

    @Autowired
    private AdminPlantService adminPlantService;

    /**
     * 获取植物列表
     *
     * @param
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('admin')")
    public JsonRequest list(@RequestParam(defaultValue = "1")Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize,String commonName,String scientificName) {
        return JsonRequest.success(adminPlantService.PlantList(pageNum,pageSize,commonName,scientificName));
    }

    /**
     * 回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('admin')")
    public JsonRequest getPlantById(@PathVariable("id") Integer id) {
        return JsonRequest.success(adminPlantService.getPlantById(id));
    }

    /**
     * 修改
     *
     * @param plantInfoVo
     * @return
     */
    @PutMapping
    @PreAuthorize("hasAnyAuthority('admin')")
    public JsonRequest planUpdate(@RequestBody PlantInfoVo plantInfoVo) {
        return adminPlantService.plantListUpdate(plantInfoVo);
    }


    /**
     * 新增
     */
    @PostMapping
    @PreAuthorize("hasAnyAuthority('admin')")
    public JsonRequest planAdd(@RequestBody PlantInfoVo plantInfoVo) {
        return JsonRequest.success(adminPlantService.insertPlantInfo(plantInfoVo));
    }
    @DeleteMapping("/{id}")
    public JsonRequest delete(@PathVariable("id") Integer id) {
        return JsonRequest.success(adminPlantService.deletePlantById(id));
    }
}
